﻿using Microsoft.AspNetCore.Mvc;
using System.Globalization;
using System.Text;

namespace Zhaoxi.SessionSample.HttpApi.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class Session01Controller : ControllerBase
    {
        [HttpGet]
        public async Task<IActionResult> Get()
        {
            var session = HttpContext.Session;
            await session.LoadAsync();

            string sessionStartTime;

            if (session.TryGetValue("SessionStartTime", out var value))
            {
                sessionStartTime = Encoding.UTF8.GetString(value);
            }
            else
            {
                sessionStartTime = DateTime.Now.ToString(CultureInfo.CurrentCulture);
                session.SetString("SessionStartTime", sessionStartTime);
            }

            var response = new
            {
                SessionId = session.Id,
                SessionStartTime = sessionStartTime,
                CurrentTime = DateTime.Now.ToString(CultureInfo.CurrentCulture)
            };

            return Ok(response);
        }
    }
}
